package com.zjj.day0425.string;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date 2025/4/25
 */
public class StringConcatenationTime {
    public static void main(String[] args) {
        int loopCount = 10000;

        long startTimeForString = System.currentTimeMillis();
        String resultForString = "";
        for (int i = 0; i < loopCount; i++) {
            resultForString = resultForString + i;
        }
        long endTimeForString = System.currentTimeMillis();
        long timeElapsedForString = endTimeForString - startTimeForString;
        System.out.println("使用 String + 拼接 " + loopCount + " 次的执行时间: " + timeElapsedForString + " 毫秒");

        long startTimeForStringBuilder = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < loopCount; i++) {
            stringBuilder.append(i);
        }
        String resultForStringBuilder = stringBuilder.toString();
        long endTimeForStringBuilder = System.currentTimeMillis();
        long timeElapsedForStringBuilder = endTimeForStringBuilder - startTimeForStringBuilder;
        System.out.println("使用 StringBuilder 拼接 " + loopCount + " 次的执行时间: " + timeElapsedForStringBuilder + " 毫秒");
    }
}